import {
  searchGoods,
  addShopCart,
  getConfirmProduct,
  createOrder,
  payOrder,
  ePay,
  getCartProductList,
  updateUserAddress
} from '@/service/getData'
const shop = {
  state: {
    cartNum: 0,
    addressId: ''
  },
  mutations: {
    SET_CART_NUM(state, data) {
      state.cartNum = data
    },
    SET_ADDRESS_ID(state, data) {
      state.addressId = data
    }
  },
  actions: {
    // 获取购物车数量
    addCartNum({ commit, state }, parameterData) {
      commit('SET_CART_NUM', parameterData)
    },

    // 获取购物车数量
    getCartNum({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        getCartProductList(parameterData)
          .then(response => {
            let cartNum = 0
            response.data.cartList.forEach(item => {
              item.goodsList.forEach(good => {
                good.sizeList.forEach(size => {
                  cartNum += size.cartNum
                })
              })
            })
            commit('SET_CART_NUM', cartNum)
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 添加/更新地址
    UpdateUserAddress({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        updateUserAddress(parameterData)
          .then(response => {
            commit('SET_ADDRESS_ID', response.data.addressId)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 搜索商品
    SearchGoods({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        searchGoods(parameterData).then(
          response => {
            resolve(response)
          },
          err => {
            reject(err)
          }
        )
      })
    },
    // 搜索商品
    AddShopCart({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        addShopCart(parameterData).then(
          response => {
            resolve(response)
          },
          err => {
            reject(err)
          }
        )
      })
    },
    //获取确认订单商品
    GetConfirmProduct({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        getConfirmProduct(parameterData).then(
          response => {
            resolve(response)
          },
          err => {
            reject(err)
          }
        )
      })
    },
    // 创建订单
    CreateOrder({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        createOrder(parameterData).then(
          response => {
            resolve(response)
          },
          err => {
            reject(err)
          }
        )
      })
    },
    //支付订单
    PayOrder({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        payOrder(parameterData).then(
          response => {
            resolve(response)
          },
          err => {
            reject(err)
          }
        )
      })
    },
    //支付订单
    EPay({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        ePay(parameterData).then(
          response => {
            resolve(response)
          },
          err => {
            reject(err)
          }
        )
      })
    }
  }
}

export default shop
